use "$basedata/estimationdata/All_T_J/principal_pref_est_data.dta", clear

egen sidx = group(ncerdc_schlcode)

local suff2 = ""

forv tt=2013/2016 {
	gen evaas_`tt' = evaas_score*(app_year==`tt')
	bys applicant_id: egen i_evaas_`tt' = min(evaas_`tt') 
	
	gen evaas_missing_`tt' = evaas_missing*(app_year==`tt')
	bys applicant_id: egen i_evaas_missing_`tt' = min(evaas_missing_`tt')
	
}

drop evaas_score evaas_missing evaas_* evaas_missing_*

gen evaas_score = 0
gen evaas_missing = 1

forv tt=2014/2017 {
	local ttt = `tt'-1
	replace evaas_score = i_evaas_`ttt' if app_year==`tt' & i_evaas_`ttt'!=.
	replace evaas_missing = i_evaas_missing_`ttt' if app_year==`tt' & i_evaas_missing_`ttt'!=.
}

replace evaas_missing = (evaas_score==0)

keep jtidx sidx app_year positive_assessment va_ma_preY_dev`suff2' va_ma_preY_dev`suff2'_T1 mu_jt_preY_mean_s_ma`suff2' mu_jt_preY_mean_s_ma`suff2'_T1 exp_2 exp_2_T1 exp_4 exp_4_T1 exp_6 exp_6_T1 ///
	exp_missing exp_missing_T1 masters masters_T1 black black_teachXblack_share black_T1 black_teachXblack_share_T1 hispanic hispanic_T1 hisp_teachXhisp_share ///
	hisp_teachXhisp_share_T1 female female_T1 sex_missing sex_missing_T1 race_missing race_missing_T1 has_va_ma_preY_dev`suff2' has_va_ma_preY_dev`suff2'_T1 titleI ///
	praxis praxis_T1 praxisMissing praxisMissing_T1 grad_degree grad_degree_T1 grad_degreeMissing grad_degreeMissing_T1 certified certified_T1 certifiedMissing certifiedMissing_T1 ///
	licensed licensed_T1 licensedMissing licensedMissing_T1 evaas_score evaas_missing
order jtidx sidx app_year positive_assessment va_ma_preY_dev`suff2' va_ma_preY_dev`suff2'_T1 mu_jt_preY_mean_s_ma`suff2' mu_jt_preY_mean_s_ma`suff2'_T1 exp_2 exp_2_T1 exp_4 exp_4_T1 exp_6 exp_6_T1 ///
	exp_missing exp_missing_T1 masters masters_T1 black black_teachXblack_share black_T1 black_teachXblack_share_T1 hispanic hispanic_T1 hisp_teachXhisp_share ///
	hisp_teachXhisp_share_T1 female female_T1 sex_missing sex_missing_T1 race_missing race_missing_T1 has_va_ma_preY_dev`suff2' has_va_ma_preY_dev`suff2'_T1 titleI ///
	praxis praxis_T1 praxisMissing praxisMissing_T1 grad_degree grad_degree_T1 grad_degreeMissing grad_degreeMissing_T1 certified certified_T1 certifiedMissing certifiedMissing_T1 ///
	licensed licensed_T1 licensedMissing licensedMissing_T1 evaas_score evaas_missing
	
	


xtset jtidx


gen VA = va_ma_preY_dev`suff2'+ mu_jt_preY_mean_s_ma`suff2'
gen VA_T1 = va_ma_preY_dev`suff2'_T1 + mu_jt_preY_mean_s_ma`suff2'_T1

gen VA_missing = (VA==0) 
gen VA_missing_T1 = VA_missing *titleI


gen evaas_score_T1 = evaas_score*titleI
gen evaas_missing_T1 = evaas_missing*titleI

keep if app_year>=2014 & app_year<=2017


forv tt=0/1 {
	
if "`tt'"== "0" {
	local if = "if titleI==0"
}
if "`tt'"== "1" {
	local if = "if titleI==1"
}


xtlogit positive_assessment black black_teachXblack_share black_T1 black_teachXblack_share_T1 hispanic hispanic_T1 hisp_teachXhisp_share ///
	hisp_teachXhisp_share_T1 female female_T1 sex_missing sex_missing_T1 race_missing race_missing_T1 exp_2 exp_2_T1 exp_4 exp_4_T1 exp_6 exp_6_T1 ///
	exp_missing exp_missing_T1 masters masters_T1 praxis praxis_T1 praxisMissing praxisMissing_T1 grad_degree grad_degree_T1 grad_degreeMissing grad_degreeMissing_T1 certified certified_T1 certifiedMissing certifiedMissing_T1 titleI VA VA_T1 VA_missing VA_missing_T1 ///
	licensed licensed_T1 licensedMissing licensedMissing_T1 evaas_score evaas_missing evaas_score_T1 evaas_missing_T1 `if', fe 
gen r2_4_`tt' = e(r2_p)
gen sampleall_`tt' = e(sample)

xtlogit positive_assessment titleI `if' & sampleall_`tt'==1, fe
gen r2_1_`tt' = e(r2_p)

xtlogit positive_assessment titleI black black_teachXblack_share black_T1 black_teachXblack_share_T1 hispanic hispanic_T1 hisp_teachXhisp_share ///
	hisp_teachXhisp_share_T1 female female_T1 sex_missing sex_missing_T1 race_missing race_missing_T1 `if' & sampleall_`tt'==1, fe
gen r2_2_`tt' = e(r2_p)

xtlogit positive_assessment titleI black black_teachXblack_share black_T1 black_teachXblack_share_T1 hispanic hispanic_T1 hisp_teachXhisp_share ///
	hisp_teachXhisp_share_T1 female female_T1 sex_missing sex_missing_T1 race_missing race_missing_T1 exp_2 exp_2_T1 exp_4 exp_4_T1 exp_6 exp_6_T1 ///
	exp_missing exp_missing_T1 masters masters_T1 praxis praxis_T1 praxisMissing praxisMissing_T1 grad_degree grad_degree_T1 grad_degreeMissing grad_degreeMissing_T1 certified certified_T1 certifiedMissing certifiedMissing_T1 ///
	licensed licensed_T1 licensedMissing licensedMissing_T1 `if' & sampleall_`tt'==1, fe
gen r2_3_`tt' = e(r2_p)

xtlogit positive_assessment titleI VA VA_T1 VA_missing VA_missing_T1 `if' &  sampleall_`tt'==1, fe
gen r2_5_`tt' = e(r2_p)


xtlogit positive_assessment titleI black black_teachXblack_share black_T1 black_teachXblack_share_T1 hispanic hispanic_T1 hisp_teachXhisp_share ///
	hisp_teachXhisp_share_T1 female female_T1 sex_missing sex_missing_T1 race_missing race_missing_T1 VA VA_T1 VA_missing VA_missing_T1 `if' & sampleall_`tt'==1, fe
gen r2_6_`tt' = e(r2_p)

xtlogit positive_assessment titleI exp_2 exp_2_T1 exp_4 exp_4_T1 exp_6 exp_6_T1 ///
	exp_missing exp_missing_T1 masters masters_T1 praxis praxis_T1 praxisMissing praxisMissing_T1 grad_degree grad_degree_T1 grad_degreeMissing grad_degreeMissing_T1 certified certified_T1 certifiedMissing certifiedMissing_T1 ///
	licensed licensed_T1 licensedMissing licensedMissing_T1 VA VA_T1 VA_missing VA_missing_T1 `if' & sampleall_`tt'==1, fe
gen r2_7_`tt' = e(r2_p)

xtlogit positive_assessment titleI exp_2 exp_2_T1 exp_4 exp_4_T1 exp_6 exp_6_T1 ///
	exp_missing exp_missing_T1 masters masters_T1 praxis praxis_T1 praxisMissing praxisMissing_T1 grad_degree grad_degree_T1 grad_degreeMissing grad_degreeMissing_T1 certified certified_T1 certifiedMissing certifiedMissing_T1 ///
	licensed licensed_T1 licensedMissing licensedMissing_T1 `if' & sampleall_`tt'==1, fe
gen r2_8_`tt' = e(r2_p)

xtlogit positive_assessment titleI evaas_score evaas_missing evaas_score_T1 evaas_missing_T1 `if' & sampleall_`tt'==1, fe
gen r2_9_`tt' = e(r2_p)

xtlogit positive_assessment black black_teachXblack_share black_T1 black_teachXblack_share_T1 hispanic hispanic_T1 hisp_teachXhisp_share ///
	hisp_teachXhisp_share_T1 female female_T1 sex_missing sex_missing_T1 race_missing race_missing_T1 exp_2 exp_2_T1 exp_4 exp_4_T1 exp_6 exp_6_T1 ///
	exp_missing exp_missing_T1 masters masters_T1 praxis praxis_T1 praxisMissing praxisMissing_T1 grad_degree grad_degree_T1 grad_degreeMissing grad_degreeMissing_T1 certified certified_T1 certifiedMissing certifiedMissing_T1 titleI VA VA_T1 VA_missing VA_missing_T1 ///
	licensed licensed_T1 licensedMissing licensedMissing_T1  `if', fe
gen r2_10_`tt' = e(r2_p)

xtlogit positive_assessment titleI VA VA_T1 VA_missing VA_missing_T1 evaas_score evaas_missing evaas_score_T1 evaas_missing_T1 `if' & sampleall_`tt'==1, fe
gen r2_11_`tt' = e(r2_p)

}

save "$temp/logit_pseudor2", replace

use "$temp/logit_pseudor2", clear

count if sampleall_0==1
global n0 = r(N)

count if sampleall_1==1
global n1 = r(N)

	local n0 : di %8.0fc $n0
	local n1 : di %8.0fc $n1

gen avar = .
gen bvar = .
gen cvar = .
gen dvar = .
gen evar = .
gen fvar = .
gen gvar = .
gen hvar = .
gen ivar = .
gen jvar = .

matrix a = (r2_2_0[1],r2_2_1[1])
matrix b = (r2_8_0[1],r2_8_1[1])
matrix c = (r2_5_0[1],r2_5_1[1])
matrix d = (r2_9_0[1],r2_9_1[1])
matrix e = (r2_3_0[1],r2_3_1[1])
matrix f = (r2_6_0[1],r2_6_1[1])
matrix g = (r2_7_0[1],r2_7_1[1])
matrix h = (r2_11_0[1],r2_11_1[1])
matrix i = (r2_10_0[1],r2_10_1[1])
matrix j = (r2_4_0[1],r2_4_1[1])

label var avar "Demographics"
label var bvar "Teacher Characteristics"
label var cvar "Value Added"
label var dvar "EVAAS"
label var evar "Demographics + Teacher Characteristics"
label var fvar "Demographics + Value Added"
label var gvar "Teacher Characteristics + Value Added"
label var hvar "EVAAS + Value Added"
label var ivar "Demographics + Teacher Characteristics + Value Added"
label var jvar "Demographics + Teacher Characteristics + EVAAS + Value Added"

mat rownames a = avar
mat rownames b = bvar
mat rownames c = cvar
mat rownames d = dvar
mat rownames e = evar
mat rownames f = fvar
mat rownames g = gvar
mat rownames h = hvar
mat rownames i = ivar
mat rownames j = jvar

matrix mfull = a\b\c\d\e\f\g\h\i\j


frmttable using "$tables/pseudo_r2s_w_evaas", statmat(mfull) replace va tex fra ///
	ctitles("","Non-Title I","Title I") ///
	sdec(3,3 \ 3,3 \3,3 \3,3 \3,3 \3,3 \3,3 \3,3\3,3\3,3  )  addrows("\hline Observations","`n0'","`n1'")